$(document).ready(function(){
	
	$('input:checkbox').click(function(){
		$(this).parent().parent().toggleClass('ui-state-highlight');
	});

	// Case: seeing a crmuser's details
	$('a#details').click(function(){
		
		// Checking if we have a crmuser selected
		var checked = $('input:checkbox:checked');
		if (checked.length <= 0) {
			alert('Vous devez sélectionner un élément !');
			return false;
		}
		
		// Redirecting user to crmusers.php
		$(this).attr('href', 'crmusers.php?id='+checked.first().val()+'&operation=edit');
		return true;
	});
	
	// Case: deleting a crmuser batch
	$('a#delete').click(function(){
		
		// Checking rights
		if (check_credentials('crmuser_delete') != 'yes'){
			alert('Vous n\'avez pas le droit d\'effectuer cette opération !');
			return false;
		}
		
		// Checking if we have at least one crmuser selected
		var checked = $('input:checkbox:checked');
		if (checked.length <= 0) {
			alert('Vous devez sélectionner au moins un élément !');
			return false;
		}
		
		// AJAXing deletion
		if (confirm('Êtes-vous sûr de vouloir supprimer '+checked.length+' élément(s) ?')) {
			var total = checked.length;
			showLoading();
			checked.each(function(){
				$.ajax({
					type:	'POST',
					url:	'process/crmuser_delete.php',
					data:	'id='+$(this).val(),
					success: function(msg){
						if (msg != 'OK')
							alert(msg);
						else
							checked.parent().parent().remove();
						addLoading(100/total);
						closeLoading();
					}
				});
			});
		}
		return false;
	});
	
	// Case: editing crmuser's contact
	$('input[name=crmuser-contact]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_contacts.php',
			data:	'id='+$('input[name=crmuser-contactId]').val(),
			success: function(msg) {
				var select = $('div#select_contact');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0) {
								$('input[name=crmuser-contact]').val('');
								$('input[name=crmuser-contactId]').val('');
							} else {
								$('input[name=crmuser-contact]').val($('span#contact-'+checked.first().val()).html());
								$('input[name=crmuser-contactId]').val(checked.first().val());
							}
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: editing crmuser's role
	$('input[name=crmuser-credentials]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_credentials.php',
			data:	'id='+$('input[name=crmuser-credentialsId]').val(),
			success: function(msg) {
				var select = $('div#select_credentials');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0) {
								$('input[name=crmuser-credentials]').val('');
								$('input[name=crmuser-credentialsId]').val('');
							} else {
								$('input[name=crmuser-credentials]').val($('span#credentials-'+checked.first().val()).html());
								$('input[name=crmuser-credentialsId]').val(checked.first().val());
							}
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: submitting a new (or edited) crmuser
	$('a#form-crmuser').click(function(){
		
		// Getting inputs
		var operation = $('input[name=crmuser-operation]'),
			id = $('input[name=crmuser-crmuserId]'),
			login = $('input[name=crmuser-login]'),
			password = $('input[name=crmuser-password]'),
			confirmation = $('input[name=crmuser-confirmation]'),
			credentials = $('input[name=crmuser-credentialsId]'),
			contact = $('input[name=crmuser-contactId]'),
			allFields = $([]).add(login).add(password).add(contact).add(credentials);
		
		// Checking rights
		if (check_credentials('crmuser_'+operation.val()) != 'yes'){
			alert('Vous n\'avez pas le droit d\'effectuer cette opération !');
			return false;
		}
		
		// Checking form validity
		allFields.removeClass('ui-state-error');
		var valid = true;
		valid = valid && check_length(login, 'Utilisateur', 1, 255);
		valid = valid && check_length(credentials, 'Rôle', 1, 10);
		valid = valid && check_length(contact, 'Contact associé', 1, 10);
		if (password.val().length > 0){
			valid = valid && check_regexp(password, rexp_password, 'Le mot de passe doit contenir au moins une lettre, un chiffre et doit contenir entre 8 et 15 caractères');
			if (valid && (password.val() != confirmation.val())){
				password.addClass('ui-state-error');
				confirmation.addClass('ui-state-error');
				alert('Les deux mots de passes sont différents !');
				valid = false;
			}
		}
		// We have errors
		if (!valid)
			return false;

		// No error: AJAX processing
		showLoading();
		$.ajax({
			type:	'POST',
			url:	'process/crmuser_'+operation.val()+'.php',
			data:	'id='+id.val()+'&login='+login.val()+'&password='+password.val()+'&credentials='+credentials.val()+'&contact='+contact.val(),
			success: function(msg){
				addLoading(100);
				history.back();
			}
		});
		return false;
	});
});